<template>
    <div class="notfound errorpage" ref="bgstar" id="bgstar">
        <img :src="src" class="myimg" alt="">
        <div class="dao" @click="gotoWhere">
             <el-progress type="circle" width="80" :percentage="percentage" :status="status" :indeterminate="true"/>
        </div>
        <div class="down" @click="gotoWhere" v-if="false"  >{{num}} S</div>
        <span v-for="(l,i) in spanNum" :key="i" class="spandot" :style="l"></span>
    </div>
</template>

<script>
import {onMounted, reactive, toRefs,watch,ref,onUnmounted} from "vue"
import {useRouter} from "vue-router"
var timer = null
export default {
    name:"notfound",
    setup(props){
        onUnmounted(()=>{
            clearInterval(timer)
            timer = null
        })
        const router = useRouter();  
        const state = reactive({
            num:5,
            src:require("@/assets/images/404.png"),
            spanNum:[],
            percentage:0,
            status:''
        })
        const bgstar = ref()

        const changenum = ()=>{
            state.num++;
        }

        const spanInit = ()=>{
            const width = bgstar.value.clientWidth;
            const height = document.getElementById('bgstar').clientHeight
            console.log(width,height)
            console.log(bgstar)
            for(var i = 0; i<520; i++){
                var x = width* Math.random() 
                var y = height* Math.random()

                var opacity = Math.random()
                var scaler = 1.5 * Math.random()
                var rate = 2.5 * Math.random() 
                
                var r = Math.random() * 255;
                var g = Math.random() * 255;
                var b = Math.random() * 255; 

                state.spanNum.push({
                    top:y+'px',
                    left:x+'px',
                    background:`rgba(${r},${g},${b},${opacity})`,
                    transform:'scale('+scaler+')',
                    animationDelay:rate+'s'
                })
            }
        }

        const gotoWhere = ()=>{
            router.push({name:'login'})
        }

        const timeDown2 = ()=>{
            timer = setInterval(()=>{
                if(state.percentage<100){  
                    state.percentage+=5; 
                }else{
                    clearInterval(timer)
                    timer = null;
                    state.status="success"
                    gotoWhere()
                }
            },250)  
        }


        const timeDown = ()=>{
            timer = setInterval(()=>{
                if(state.num>0){
                    state.num--
                }else{
                    clearInterval(timer)
                    timer = null;
                    // 跳转
                    gotoWhere()
                }
            },1000)
        }

        // 生命周期
        onMounted(()=>{
            console.log("404 setup")
            spanInit()
            // timeDown()
            timeDown2()
        })

        return {
            ...toRefs(state), 
            changenum,
            bgstar,
            gotoWhere
        }
    },
    mounted(){

    }
}
</script>


<style lang="scss" scoped >
.errorpage{
  width:100%;
  height:100%;
  background: #000;
  position: relative;
  overflow: hidden;
  .down{
    position:absolute;
    left:20px;
    top:20px;
    border:1px solid #fff;
    color:#fff;
    width:70px;
    height:30px;
    border-radius:10px;
    text-align:center;
    line-height:30px;
  }
  .dao{
    position: absolute;
    top:20px;
    right:20px;
   
  }
  .myimg{
    width:270px;
    height:270px;
    position: absolute;
    top:0;
    right:0;
    left:0;
    bottom:0;
    margin:auto;
    z-index:10;
  }
  .spandot{
    position: absolute;
    z-index: 100;
    width: 4px;
    height:4px;
    border-radius: 50%;
    animation: flash 3s infinite alternate;

  }
   @keyframes flash {
        from{
            opacity: 0;
        }
        to{
            opacity: 0.5;
        }
    }
}
</style>